package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.LRegion;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LEmployee;
import com.ruoyi.system.service.ILEmployeeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工Controller
 *
 * @author zyb
 * @date 2022-01-02
 */
@RestController
@RequestMapping("/system/employee")
public class LEmployeeController extends BaseController
{
    @Autowired
    private ILEmployeeService lEmployeeService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询员工列表
     */
    @PreAuthorize("@ss.hasPermi('system:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(LEmployee lEmployee)
    {
        startPage();
        List<LEmployee> list = lEmployeeService.selectLEmployeeList(lEmployee);
        return getDataTable(list);
    }

    /**
     * 导出员工列表
     */
    @PreAuthorize("@ss.hasPermi('system:employee:export')")
    @Log(title = "员工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LEmployee lEmployee)
    {
        List<LEmployee> list = lEmployeeService.selectLEmployeeList(lEmployee);
        ExcelUtil<LEmployee> util = new ExcelUtil<LEmployee>(LEmployee.class);
        util.exportExcel(response, list, "员工数据");
    }

    /**
     * 获取员工详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:employee:query')")
    @GetMapping(value = "/{empid}")
    public AjaxResult getInfo(@PathVariable("empid") Integer empid)
    {
        return AjaxResult.success(lEmployeeService.selectLEmployeeByEmpid(empid));
    }

    /**
     * 新增员工
     */
    @PreAuthorize("@ss.hasPermi('system:employee:add')")
    @Log(title = "员工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LEmployee lEmployee)
    {
        return toAjax(lEmployeeService.insertLEmployee(lEmployee));
    }

    /**
     * 修改员工
     */
    @PreAuthorize("@ss.hasPermi('system:employee:edit')")
    @Log(title = "员工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LEmployee lEmployee)
    {
        return toAjax(lEmployeeService.updateLEmployee(lEmployee));
    }

    /**
     * 删除员工
     */
    @PreAuthorize("@ss.hasPermi('system:employee:remove')")
    @Log(title = "员工", businessType = BusinessType.DELETE)
	@DeleteMapping("/{empids}")
    public AjaxResult remove(@PathVariable Integer[] empids)
    {
        return toAjax(lEmployeeService.deleteLEmployeeByEmpids(empids));
    }

    @GetMapping("/list/all/driver")
    public AjaxResult listAllDriver()
    {
        List<LEmployee> list =redisCache.getCacheList("driver");
        if(list.size()==0){
            list = lEmployeeService.getRedisList(3);
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/list/all/salesman")
    public AjaxResult listAllSalesman()
    {
        List<LEmployee> list = redisCache.getCacheList("salesman");
        if(list.size()==0){
            list = lEmployeeService.getRedisList(2);
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/list/all")
    public AjaxResult listAll()
    {
        List<LEmployee> list = redisCache.getCacheList("employee");
        if(list.size()==0){
            list = lEmployeeService.getRedisList(1);
        }
        return AjaxResult.success(list);
    }
}
